﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Media;

namespace ETC.HallICElan2In1.Helpers
{
    public class WinAPIManager
    {
        [DllImport("user32.dll")]
        public static extern bool SetWindowText(IntPtr hwnd, String lpStrjng);

        [DllImport("user32.dll")]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        //获取桌面窗口句柄
        [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
        public static extern IntPtr GetDesktopWindow();

        [DllImport("user32.dll")]
        public static extern IntPtr GetWindow(IntPtr hWnd, int cmd);

        [DllImport("User32.dll", EntryPoint = "GetWindowRect", CharSet = CharSet.Auto)]
        public static extern bool GetWindowRect(IntPtr hWnd, out System.Drawing.Rectangle lpRect);

        [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        // 函数说明：在窗口列表中寻找与指定条件相符的第一个子窗口
        // 命名空间　using System.Runtime.InteropServices;
        // 参数说明　
        // hwndParent IntPtr ，父窗口句柄，如果hwndParent为 0 ，则函数以桌面窗口为父窗口，查找桌面窗口的所有子窗口。
        // hwndChildAfter IntPtr ，子窗口句柄，查找从在Z序中的下一个子窗口开始。子窗口必须为hwndParent窗口的直接子窗口而非后代窗口。如果HwndChildAfter为NULL，查找从hwndParent的第一个子窗口开始。如果hwndParent 和 hwndChildAfter同时为NULL，则函数查找所有的顶层窗口及消息窗口。
        // lpszClass string ，控件类名
        // lpszWindow string ，控件标题，如果该参数为 NULL，则为所有窗口全匹配。
        // 返回值：控件句柄。
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        // 函数说明：在窗口列表中寻找与指定条件相符的第一个窗口
        // 命名空间　using System.Runtime.InteropServices;
        // 参数说明　
        // lpClassName　String，窗口类名
        // lpWindowName String，窗口标题
        public static extern IntPtr FindWindow(string IpClassName, string IpWindowName);

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, string IParam);

        [DllImport("User32.dll", EntryPoint = "SendMessage", SetLastError = true)]
        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int wMsg, uint wParam, uint lParam);

        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);

        [DllImport("user32.dll", EntryPoint = "CloseWindow")]
        public static extern bool CloseWindow(IntPtr hWnd);

        //[DllImport("user32.dll", EntryPoint = "EnumWindows")]
        //public static extern IntPtr EnumWindows(ETC.BUI.EDIDTest2In1.MainWindow.EnumWindowsProc lpEnumFunc, string lParam);

        //[DllImport("user32.dll", EntryPoint = "EnumChildWindows")]
        //public static extern IntPtr EnumChildWindows(IntPtr hWndParent, ETC.BUI.EDIDTest2In1.MainWindow.EnumWindowsProc lpEnumFunc, string lParam);

        [DllImport("user32.dll", EntryPoint = "GetClassName")]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder IpClassName, int nMaxCount);

        [DllImport("user32.dll", EntryPoint = "mouse_event")]
        public static extern void mouse_event(uint flags, int dx, int dy, uint data, int extraInfo);

        [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
        public static extern bool SetCursorPos(int X, int Y);

        [DllImport("gdi32.dll", EntryPoint = "GetPixel")]
        public static extern Color GetPixel(IntPtr hdc, int nXPos, int nYPos);

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern bool SetWindowPos(IntPtr hWnd, int hWndlnsertAfter, int X, int Y, int cx, int cy, int flag);

        [DllImport("user32.dll", EntryPoint = "SetFocus")]
        public static extern IntPtr SetFocus(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImportAttribute("user32.dll", EntryPoint = "SendMessageW")]
        public static extern int SendMessageW(IntPtr hWnd, uint Msg, uint wParam, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "IsWindowEnabled")]
        public static extern bool IsWindowEnabled(IntPtr hWnd);

        //窗口的可视状态
        [DllImport("user32.dll", EntryPoint = "IsWindowVisible")]
        public static extern bool IsWindowVisible(IntPtr hWnd);


        // 函数功能：枚举一个父窗口的所有子窗口。
        // 命名空间　using System.Runtime.InteropServices;
        // 参数说明
        // hWndParent IntPtr 父窗口句柄
        // lpfn CallBack 回调函数的地址
        // lParam int 自定义的参数
        // 注意：回调函数的返回值将会影响到这个API函数的行为。如果回调函数返回true，则枚举继续直到枚举完成；如果返回false，则将会中止枚举。
        // 其中CallBack是这样的一个委托：public delegate bool CallBack(IntPtr hwnd, int lParam); 如果 CallBack 返回的是true，则会继续枚举，否则就会终止枚举。
        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);

        public delegate bool CallBack(IntPtr hwnd, int lParam);

        [DllImport("user32.dll", EntryPoint = "EnumChildWindows")]
        public static extern IntPtr EnumChildWindows(IntPtr hWndParent, ETC.HallICElan2In1.MainWindow.EnumWindowsProc lpEnumFunc, string lParam);
    }
}
